package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.common.query.PlaneTicketQueryObject;
import cn.wolfcode.luowowo.flight.domain.PlaneTicket;
import cn.wolfcode.luowowo.flight.service.IFlightService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/flight")
public class FlightController {

    @Reference
    private IFlightService flightService;

    @Reference
    private IDestinatonService destinatonService;


    @RequestMapping("")
    public String flight(Model model) throws IOException {
        List<PlaneTicket> list = flightService.list().subList(0, 15);
        model.addAttribute("list", list);
        return "flight/flight";
    }

    @RequestMapping("/list")
    public String search(Model model, @ModelAttribute("qo") PlaneTicketQueryObject qo) throws IOException {
        String url = "https://www.mafengwo.cn/flight/rest/citySuggest/?filter[prefix]=";
        String departCode = getCode(url, qo.getSource());
        String destCode = getCode(url, qo.getDest());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(qo.getTime());

        url = "https://www.mafengwo.cn/flight/rest/flightlist/?filter[departCode]=" + departCode + "&filter[destCode]=" + destCode + "&filter[departDate]=" + time + "&filter[destDate]=&filter[otaId]=101";
        System.out.println(url);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        JSONObject jsonObject = JSON.parseObject(result);
        Map data = (Map) jsonObject.get("data");
        Map ex = (Map) data.get("ex");
        List<Map<String, Object>> flights = (List<Map<String, Object>>) ex.get("flights");
        model.addAttribute("list", flights);
        for (Map<String, Object> flight : flights) {
            String lastd = (Integer.valueOf((String) flight.get("arrTime")) - Integer.valueOf((String) flight.get("depTime"))) + "";
            List seat_infos = (List) flight.get("seat_infos");
            Map o = (Map) seat_infos.get(0);
            Integer discount = Integer.valueOf((Integer) o.get("discount"));
            flight.put("last", lastd);
            Integer par_price = Integer.valueOf((Integer) flight.get("base_price"));
            flight.put("par_price", discount * par_price + "");

        }
        return "flight/list";
    }

    @RequestMapping("/book")
    public String book() {
        return "flight/book";
    }


    private static String getCode(String url, String city) {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url + city);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity, "UTF-8");
                Map map = JSON.parseObject(result);
                Map data = (Map) map.get("data");
                List ex = (List) data.get("ex");
                Map o = (Map) ex.get(0);
                String c = (String) o.get("c");
                return c;
            }
            httpGet.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
